//
//  PersonalProfileViewController.m
//  ChatForHot
//
//  Created by wr on 2018/10/10.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalProfileViewController.h"
#import "PersonalCell.h"
#import "PersonalProfileHeaderView.h"
#import "PersonalProfileViewModel.h"
#import "BaseTableView.h"
#import "SelectView.h"
#import "PerssonalProfileEditCell.h"
#import "PersonalProfileSiginViewController.h"

@interface PersonalProfileViewController ()<UITableViewDelegate, UITableViewDataSource,SelectDelegate,ProfileEditCellDelegate>

@property (nonatomic, strong) PersonalProfileViewModel *viewModel;
@property (nonatomic, strong) BaseTableView *HOTCHATtableView;
@property (nonatomic, strong) SelectView *HOTCHATselectView;
@property (nonatomic, strong) UserInfoModel *model;
@property (nonatomic, strong) UIButton *HOTCHATsaveBtn;
@property (nonatomic, strong) PersonalProfileHeaderView *HOTCHATheaderView;
@end

@implementation PersonalProfileViewController

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    
    self.viewModel = [[PersonalProfileViewModel alloc] init];
}

- (void)HOTCHATInsertSubViews {
    [super HOTCHATInsertSubViews];
    @weakify(self)
    
    self.HOTCHATtableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.HOTCHATtableView.delegate = self;
    self.HOTCHATtableView.dataSource = self;
    self.HOTCHATtableView.tableHeaderView.height = 16;
    [self.HOTCHATtableView registerClass:[PersonalCell class] forCellReuseIdentifier:@"PersonalCell"];
    [self.HOTCHATtableView registerClass:[PerssonalProfileEditCell class] forCellReuseIdentifier:@"PerssonalProfileEditCell"];
    [self.view addSubview:self.HOTCHATtableView];
    
    self.HOTCHATheaderView = [[PersonalProfileHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 315)];
    self.HOTCHATtableView.tableHeaderView = self.HOTCHATheaderView;
    
    PersonalProfileSiginViewController *vc = [[PersonalProfileSiginViewController alloc] init];
    [self.HOTCHATheaderView.introDidSelectSubject subscribeNext:^(id x) {
        @strongify(self)
        vc.model = self.model;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    [vc.shouldReloadDataSubejct subscribeNext:^(id x) {
        @strongify(self)
        self.HOTCHATheaderView.HOTCHATinrtoMesgLabel.text = self.model.signature;
    }];
    
    self.HOTCHATsaveBtn = [[UIButton alloc]init];
    [self.HOTCHATsaveBtn setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_n"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_h"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateHighlighted];
    [self.HOTCHATsaveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"SAVEBUTTON", nil) forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn addTarget:self action:@selector(HOTCHATProfilesaveAction) forControlEvents:UIControlEventTouchUpInside];
    self.HOTCHATsaveBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:self.HOTCHATsaveBtn];
}

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super HOTCHATsetNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.title = NSLocalizedString(@"person6_1", nil);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.HOTCHATtableView.frame = self.view.bounds;
    self.HOTCHATtableView.height = self.view.bounds.size.height - 44 - 10 - 6;

    self.HOTCHATsaveBtn.size = CGSizeMake(kScreenWidth - 30, 44);
    self.HOTCHATsaveBtn.top = self.HOTCHATtableView.bottom + 6;
    self.HOTCHATsaveBtn.left = 15;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    @weakify(self)
    [self.viewModel.requestUserInfoCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
            @strongify(self)
            [self HOTCHATstopLoading];
            self.model = self.viewModel.model;
            self.HOTCHATheaderView.model = self.model;
            [self.HOTCHATtableView reloadData];
        } error:^(NSError *error) {
            @strongify(self)
            [self HOTCHATstopLoading];
        }];
    }];
    
    [self.viewModel.requestSaveUserInfoCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
            @strongify(self)
            [self HOTCHATstopLoading];
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(NSError *error) {
            @strongify(self)
            [self HOTCHATstopLoading];
        }];
    }];
    
    [self HOTCHATstartLoading];
    [self.viewModel.requestUserInfoCommand execute:nil];
}

- (void)HOTCHATProfilesaveAction {
    [self HOTCHATstartLoading];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    APPINFO *appInfo = [APPINFO defaultManager];
    [dic setValue:appInfo.accountid forKey:@"accountid"];
    [dic setValue:self.model.nickName  forKey:@"nickName"];
    [dic setValue:self.model.age forKey:@"age"];
    [dic setValue:self.model.signature  forKey:@"signature"];
    
    [dic setValue:self.model.height forKey:@"height"];
    [dic setValue:self.model.minFeet forKey:@"minFeet"];
    [dic setValue:self.model.maxFeet forKey:@"maxFeet"];
    [dic setValue:self.model.weight forKey:@"weight"];
    [dic setValue:self.model.income forKey:@"income"];
    [dic setValue:self.model.constellation forKey:@"constellation"];
    
    [dic setValue:self.model.mobile forKey:@"mobile"];
    [dic setValue:self.model.facebook forKey:@"facebook"];
    [dic setValue:self.model.whatApp forKey:@"whatApp"];
    [dic setValue:self.model.other forKey:@"other"];
    
    [self.viewModel.requestSaveUserInfoCommand execute:dic];
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.viewModel.cellDataSource.count;
    }else {
        return self.viewModel.bottomCellDataSource.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PersonalCellModel *row = [self.viewModel.cellDataSource objectOrNilAtIndex:indexPath.row];
        return row.cellHeight;
    }else {
        PersonalCellModel *row = [self.viewModel.bottomCellDataSource objectOrNilAtIndex:indexPath.row];
        return row.cellHeight;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PersonalCellModel *cellModel = [self.viewModel.cellDataSource objectOrNilAtIndex:indexPath.row];
        PersonalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonalCell"];
        [cell HOTCHATbind:cellModel];
        return cell;
    }else {
        PersonalCellModel *cellModel = [self.viewModel.bottomCellDataSource objectOrNilAtIndex:indexPath.row];
        PerssonalProfileEditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PerssonalProfileEditCell"];
        cell.delegate = self;
        [cell HOTCHATbind:cellModel];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        NSInteger rowIndex = indexPath.row + 1;
        [self.view endEditing:YES];
        if (rowIndex == 0) {
            [self.HOTCHATselectView  HOTCHATcloseTheView];
        }else{
            [self.HOTCHATselectView setMsgType:rowIndex model:self.model];
            [self.HOTCHATselectView HOTCHATshowTheView:[UIApplication sharedApplication].keyWindow];
        }
    }
}

- (void)HOTCHATselectedMsg:(NSString *)msg type:(MsgType)type num1:(NSInteger)num1 num2:(NSInteger)num2{
    APPINFO *appinfo = [APPINFO defaultManager];
    switch (type) {
        case AgeMsg:
            self.model.age = msg;
            appinfo.age = msg;
            break;
        case HeightMsg:
            self.model.height = msg;
            self.model.minFeet = [NSString stringWithFormat:@"%zd",num1];
            self.model.maxFeet = [NSString stringWithFormat:@"%zd",num2];
            appinfo.height = msg;
            appinfo.minFeet = [NSString stringWithFormat:@"%zd",num1];
            appinfo.maxFeet = [NSString stringWithFormat:@"%zd",num2];
            break;
        case WeightMsg:
            self.model.weight = msg;
            appinfo.weight = msg;
            break;
        case StarMsg:
            self.model.constellation = msg;
            appinfo.constellation = msg;
            break;
        case IncomeMsg:
            self.model.income = msg;
            appinfo.income = msg;
            break;
        case JobMsg:
            self.model.occupation = msg;
            appinfo.occupation = msg;
            break;
            
        default:
            break;
    }
    [self.HOTCHATtableView reloadData];
}

- (void)HOTCHATsetText:(NSString *)text tag:(NSInteger)tag {
    switch (tag) {
        case 0:
        {
         self.model.mobile = [text HOTCHATmaxLength:20];
        }
            break;
        case 1:
        {
         self.model.facebook = [text HOTCHATmaxLength:30];
        }
            break;
        case 2:
        {
            self.model.whatApp = [text HOTCHATmaxLength:30];
        }
            break;
        case 3:
        {
            self.model.other = [text HOTCHATmaxLength:30];
        }
            break;
        default:
            break;
    }
}

- (SelectView *)HOTCHATselectView{
    
    if (_HOTCHATselectView == nil) {
        
        _HOTCHATselectView = [[SelectView alloc] init];
        _HOTCHATselectView.delegate = self;
    }
    return _HOTCHATselectView;
}
@end
